home *** CD-ROM | disk | FTP | other *** search
/ EDUCORP 8 / Educorp2Compilation.sit / educorp2 / Demos / Aztec Source Level Debugger / demo / explor.c < prev    next >
Encoding:
Text File  |  1988-06-22  |  9.4 KB  |  486 lines

  1. /*
  2.  *                Explorer - Version 1.0
  3.  *
  4.  *            A Desk Accessory for the Macintosh
  5.  *
  6.  *    Copyright (C) 1984 by Manx Software Systems, Inc.
  7.  *        May be used, but not sold without permission.
  8.  *
  9.  *    written by Jim Goodnow II, December 7-8, 1984
  10.  */
  11.  
  12. #asm
  13. _main
  14.     dc.w    $2400            ;ctl-enable, need time
  15.     dc.w    5*60            ;update every 5 seconds
  16.     dc.w    $000a            ;detect mouse and key down events
  17.     dc.w    -777            ;menu ID number (must be negative)
  18.  
  19.     dc.w    _open-_main        ;open routine
  20.     dc.w    _nop-_main        ;prime routine
  21.     dc.w    _control-_main    ;control routine
  22.     dc.w    _nop-_main        ;status routine
  23.     dc.w    _close-_main        ;close routine
  24.  
  25. _title
  26. ;    dc.b    8                ;needed for -lc3, remove for -lc2
  27.     dc.b    "Explorer"
  28.     dc.b    0                ;needed for -lc2, doesn't hurt for -lc3
  29.     ds        0                ;for alignment
  30.  
  31.     public    __Uend,__Dorg,__Cend
  32.  
  33. _save
  34.     lea        _main,a4                                    ;set up globals -- ech 1/88
  35.     move.l    a4,d0                                        ;remember the _main
  36.     add.l    #(__Cend-_main+$8000),a4
  37.     move.l    a0,_Pbp                                        ;save pb pointer
  38.     move.l    a1,_Dp                                        ;save DCtlEntry pointer
  39.     lea        __Uend,a0
  40.     move.w    (a0),d1        ; get reloc count
  41.     beq        .savedone    ; nothing to reloc
  42.     sub.l    2(a0),d0    ; minus old value of _main gives reloc delta
  43.     beq        .savedone    ; haven't moved since last reloc
  44. ; first time thru loop updates the saved _main
  45. .savelp
  46.     add.w    #2,a0
  47.     add.l    d0,(a0)+
  48.     dbra    d1,.savelp
  49. .savedone
  50.     rts
  51.  
  52. _restore
  53.     move.l    _Pbp,a0
  54.     rts
  55. #endasm
  56.  
  57. #define    _DRIVER
  58. #define    SMALL_MEM
  59. #include    <Quickdraw.h>
  60. #include    <ToolUtils.h>
  61. #include    <Windows.h>
  62. #include    <Memory.h>
  63. #include    <Menus.h>
  64. #include    <Controls.h>
  65. #undef SMALL_MEM
  66. #include    <OSUtils.h>
  67. #include    <Events.h>
  68. #include    <TextEdit.h>
  69. #include    <Files.h>
  70. #include    <Devices.h>
  71. #include    <Desk.h>
  72.  
  73. #define    NLINES    16                    /* number of lines in window    */
  74. #define    MENUID    -777                /* must be negative                */
  75. #define    MEMTOP    (*(long *)0x108)    /* top of memory 128 or 512K    */
  76. #define    SP        (*(struct storage **)Dp->dCtlStorage)
  77.  
  78. DCtlPtr Dp;
  79. CntrlParam *Pbp;
  80.  
  81. Rect Wind_rect = {100, 150, 275, 360};
  82. Rect Scrl_rect = {-1, 194, 176, 211};
  83. Rect Cont_rect = {0, 0, 176, 194};
  84. Rect Full_rect = {0, 0, 176, 211};
  85. Rect Edit_rect = {-1, 44, 12, 90};
  86.  
  87. Cursor Ibeam = {
  88. 0x3838, 0x3C78, 0x0280, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100,
  89. 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0280, 0x3C78, 0x3838,
  90. 0x3838, 0x3C78, 0x0280, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100,
  91. 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0280, 0x3C78, 0x3838,
  92. 0x0008, 0x0008 };
  93.  
  94. struct storage {
  95.     MenuHandle        menu;
  96.     ControlHandle    vscrl;
  97.     char *            where;
  98.     short            autoupdate;
  99.     short            size;
  100.     short            incr;
  101.     TEHandle        hte;
  102. };
  103.  
  104. open()
  105. {
  106.     register WindowPtr wp;
  107.     register DCtlPtr dp;
  108.     register struct storage *sp;
  109.     Rect r;
  110.     GrafPtr savport;
  111.     extern char title[];
  112.     struct windowpeek {
  113.         GrafPort    port;
  114.         int            windowKind;
  115.     };
  116.  
  117.     save();
  118.     dp = Dp;
  119.     if (dp->dCtlWindow == 0) {
  120.         HLock(dp->dCtlStorage = NewHandle((long)sizeof(struct storage)));
  121.         sp = SP;
  122.         dp->dCtlWindow =
  123.         wp = NewWindow(0L, &Wind_rect, title, TRUE, noGrowDocProc, -1L, TRUE, 0L);
  124.         ((struct windowpeek *)wp)->windowKind = dp->dCtlRefNum;
  125.         sp->vscrl = NewControl(wp, &Scrl_rect, "", FALSE, 0, 0, 0x7fff, scrollBarProc, 0L);
  126.         wp->txFont = 4;                /* Monaco 9 */
  127.         wp->txSize = 9;
  128.         wp->txMode = srcCopy;
  129.         GetPort(&savport);
  130.         SetPort(wp);
  131.         r = Edit_rect;
  132.         InsetRect(&r, 4, 1);
  133.         sp->hte = TENew(&r, &r);
  134.         SetPort(savport);
  135.         sp->size = 8;
  136.         sp->where = -1;
  137.         sp->autoupdate = 0;
  138.         sp->incr = MEMTOP / 0x8000L;
  139.         sp->menu = NewMenu(MENUID, "Explorer");
  140.         AppendMenu(sp->menu,
  141.             "Auto-Refresh;Hand-Refresh;(-;Hexadecimal!\x12;Ascii");
  142.         HUnlock(dp->dCtlStorage);
  143.     }
  144.     restore();
  145.     return(0);
  146. }
  147.  
  148. close()
  149. {
  150.     register DCtlPtr dp;
  151.  
  152.     save();
  153.     dp = Dp;
  154.     TEDispose(SP->hte);
  155.     DisposeWindow(dp->dCtlWindow);
  156.     dp->dCtlWindow = 0;
  157.     DeleteMenu(MENUID);
  158.     DrawMenuBar();
  159.     DisposHandle(SP->menu);
  160.     DisposHandle(dp->dCtlStorage);
  161.     restore();
  162.     return(0);
  163. }
  164.  
  165. nop()
  166. {
  167.     return(0);
  168. }
  169.  
  170. control()
  171. {
  172.     register struct storage *sp;
  173.     Point pt;
  174.     register int item;
  175.  
  176.     save();
  177.     HLock(Dp->dCtlStorage);
  178.     sp = SP;
  179.     SetPort(Dp->dCtlWindow);
  180.     switch(Pbp->csCode) {
  181.     case accEvent:
  182.         doevent(sp, *(EventRecord **)Pbp->csParam);
  183.         break;
  184.     case accRun:
  185.         if (sp->autoupdate)
  186.             draw_wind();
  187.         break;
  188.     case accCursor:
  189.         TEIdle(sp->hte);
  190.         GetMouse(&pt);
  191.         if (PtInRect(&pt, &Edit_rect))
  192.             SetCursor(&Ibeam);
  193.         else
  194.             InitCursor();
  195.         break;
  196.     case accMenu:
  197.         switch(item = ((int *)Pbp->csParam)[1]) {
  198.         case 1:                                /* auto refresh        */
  199.             sp->autoupdate ^= TRUE;
  200.             CheckItem(sp->menu, 1, sp->autoupdate);
  201.             break;
  202.         case 2:                                /* manual refresh    */
  203.             draw_wind();
  204.             break;
  205.         case 4:
  206.         case 5:
  207.             sp->size = item==4?8:16;
  208.             CheckItem(sp->menu, 4, item==4?TRUE:FALSE);
  209.             CheckItem(sp->menu, 5, item==5?TRUE:FALSE);
  210.             EraseRect(&Cont_rect);
  211.             draw_wind();
  212.             break;
  213.         }
  214.         break;
  215.     case accUndo:
  216.         break;
  217.     case accCut:
  218.         TECut(sp->hte);
  219.         break;
  220.     case accCopy:
  221.         TECopy(sp->hte);
  222.         break;
  223.     case accPaste:
  224.         TEPaste(sp->hte);
  225.         break;
  226.     case accClear:
  227.         TEDelete(sp->hte);
  228.         break;
  229.     }
  230.     HUnlock(Dp->dCtlStorage);
  231.     restore();
  232.     return(0);
  233. }
  234.  
  235. doevent(sp, ep)
  236. register struct storage *sp;
  237. register EventRecord *ep;
  238. {
  239.     register int c;
  240.     register long l;
  241.     ControlHandle chdl;
  242.     pascal void scrlup(), scrldn();
  243.     long xtol();
  244.  
  245.     switch(ep->what) {
  246.     case keyDown:
  247.         if ((ep->modifiers & (cmdKey | optionKey)) == 0) {
  248.             c = (char)ep->message;
  249.             if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || c == 8) {
  250.                 TEKey(c, sp->hte);
  251.                 break;
  252.             }
  253.             if ((c == '\r' || c == 3) && (l = xtol(sp->hte)) != -1) {
  254.                 sp->where = (char *)l;
  255.                 TESetSelect(0L, 1000L, sp->hte);
  256.                 draw_wind();
  257.                 break;
  258.             }
  259.         }
  260.         SysBeep(2);
  261.         break;
  262.     case mouseDown:
  263.         GlobalToLocal(&ep->where);
  264.         if (PtInRect(&ep->where, &Cont_rect)) {
  265.             if (PtInRect(&ep->where, &Edit_rect))
  266.                 TEClick(&ep->where, ep->modifiers&shiftKey?TRUE:FALSE, sp->hte);
  267.         }
  268.         else {
  269.             c = FindControl(&ep->where, Dp->dCtlWindow, &chdl);
  270.             switch(c) {
  271.             case inUpButton:
  272.                 TrackControl(chdl, &ep->where, scrlup);
  273.                 break;
  274.             case inDownButton:
  275.                 TrackControl(chdl, &ep->where, scrldn);
  276.                 break;
  277.             case inPageUp:
  278.                 scrlpage(chdl, c, -NLINES);
  279.                 break;
  280.             case inPageDown:
  281.                 scrlpage(chdl, c, NLINES);
  282.                 break;
  283.             case inThumb:
  284.                 TrackControl(chdl, &ep->where, 0L);
  285.                 sp->where = (char *)((long)sp->incr * GetCtlValue(chdl));
  286.                 draw_wind();
  287.                 break;
  288.             }
  289.         }
  290.         break;
  291.     case activateEvt:
  292.         if (ep->modifiers&1) {
  293.             if (sp->where == -1) {
  294.                 signature();
  295.                 sp->where = 0;
  296.             }
  297.             InsertMenu(sp->menu, 0);
  298.             ShowControl(sp->vscrl);
  299.             TEActivate(sp->hte);
  300.         }
  301.         else {
  302.             DeleteMenu(MENUID);
  303.             HideControl(sp->vscrl);
  304.             TEDeactivate(sp->hte);
  305.         }
  306.         DrawMenuBar();
  307.         break;
  308.     case updateEvt:
  309.         BeginUpdate(ep->message);
  310.         draw_wind();
  311.         DrawControls(ep->message);
  312.         EndUpdate(ep->message);
  313.         break;
  314.     }
  315. }
  316.  
  317. signature()
  318. {
  319.     register WindowPtr wp;
  320.     register long tick;
  321.  
  322.     wp = Dp->dCtlWindow;
  323.     wp->txFont = 0;
  324.     wp->txSize = 0;
  325.     MoveTo(70, 20);
  326.     DrawString("version 2");
  327.     MoveTo(90, 40);
  328.     DrawString("by");
  329.     MoveTo(50,60);
  330.     DrawString("Jim Goodnow II");
  331.     MoveTo(55, 80);
  332.     DrawString("using Aztec C");
  333.     MoveTo(10, 110);
  334.     DrawString("Manx ");
  335.     Move(0, 20);
  336.     DrawString("Software ");
  337.     Move(0, 20);
  338.     DrawString("Systems");
  339.     Move(0, 20);
  340.     DrawString("Inc.");
  341.     for (tick=TickCount()+100;TickCount() < tick;)
  342.         ;
  343.     EraseRect(&wp->portRect);
  344.     wp->txFont = 4;
  345.     wp->txSize = 9;
  346. }
  347.  
  348. draw_wind()
  349. {
  350.     register unsigned char *cp, *wp;
  351.     register unsigned long l;
  352.     register int k, i, j;
  353.     struct storage *sp;
  354.     char buf[40];
  355.     static char hex[] = "0123456789abcdef";
  356.  
  357.     sp = SP;
  358.     l = (unsigned long)sp->where;
  359.     if (l < 0)
  360.         l = 0;
  361.     if (l > MEMTOP - sp->size*NLINES)
  362.         l = MEMTOP - sp->size * NLINES;
  363.     wp = (unsigned char *) sp->where = (unsigned char *) l;
  364.     SetCtlValue(sp->vscrl, (int)(l/sp->incr));
  365.     RectRgn(Dp->dCtlWindow->clipRgn, &Cont_rect);
  366.     MoveTo(4, 9);
  367.     DrawString("Start: ");
  368.     TEUpdate(&Edit_rect, sp->hte);
  369.     FrameRect(&Edit_rect);
  370.     for (i=0;i<NLINES;i++) {
  371.         k = i * sp->size;
  372.         MoveTo(4, i*10+24);
  373.         cp = (unsigned char *) buf;
  374.         l =(unsigned long) (wp + k);
  375.         for (j=5;j>=0;l>>=4)
  376.             cp[j--] = hex[l%16];
  377.         cp += 6;
  378.         *cp++ = ':';
  379.         if (sp->size == 8) {
  380.             for (j=0;j<8;j++) {
  381.                 *cp++ = ' ';
  382.                 *cp++ = hex[wp[k+j]/16];
  383.                 *cp++ = hex[wp[k+j]%16];
  384.             }
  385.         }
  386.         *cp = 0;
  387.         DrawString(buf);
  388.         if (sp->size == 16) {
  389.             cp = (unsigned char *)buf;
  390.             *cp++ = ' ';
  391.             for (j=0;j<16;j++) {
  392.                 if (wp[k+j] > 0x1f && wp[k+j] < 0x80)
  393.                     *cp++ = wp[k+j];
  394.                 else
  395.                     *cp++ = '.';
  396.             }
  397.             *cp = 0;
  398.             DrawString(buf);
  399.         }
  400.     }
  401.     RectRgn(Dp->dCtlWindow->clipRgn, &Full_rect);
  402. }
  403.  
  404. pascal void
  405. scrlup(chdl, code)
  406. ControlHandle chdl;
  407. int code;
  408. {
  409.     scroll(chdl, code, inUpButton);
  410. }
  411.  
  412. pascal void
  413. scrldn(chdl, code)
  414. ControlHandle chdl;
  415. int code;
  416. {
  417.     scroll(chdl, code, inDownButton);
  418. }
  419.  
  420. scroll(chdl, code, where)
  421. ControlHandle chdl;
  422. {
  423.     register struct storage *sp;
  424.  
  425. #asm
  426.     move.l    a4,-(sp)
  427.     lea        _main,a4                                        ;set up globals -- ech 1/88
  428.     add.l    #(__Cend-_main+$8000),a4
  429. #endasm
  430.     if (code == where) {
  431.         sp = SP;
  432.         if (where == inUpButton)
  433.             sp->where -= sp->size;
  434.         else
  435.             sp->where += sp->size;
  436.         draw_wind();
  437.     }
  438.     ;
  439. #asm
  440.     move.l    (sp)+,a4
  441. #endasm
  442. }
  443.  
  444. scrlpage(chdl, code, amount)
  445. ControlHandle chdl;
  446. {
  447.     Point pt;
  448.     struct storage *sp;
  449.  
  450.     sp = SP;
  451.     do {
  452.         GetMouse(&pt);
  453.         if (TestControl(chdl, &pt) == code) {
  454.             sp->where += sp->size*amount;
  455.             draw_wind();
  456.         }
  457.     } while (StillDown());
  458. }
  459.  
  460. long
  461. xtol(hte)
  462. TEHandle hte;
  463. {
  464.     register char *cp;
  465.     register long i = 0, l = 0;
  466.     register int c, n;
  467.  
  468.     n = (*hte)->teLength;
  469.     cp = *(*hte)->hText;
  470.     while (n--) {
  471.         c = *cp++;
  472.         if (c >= 'a' && c <= 'f')
  473.             c -= 'a' - 10;
  474.         else if (c >= '0' && c <= '9')
  475.             c -= '0';
  476.         else {
  477.             TESetSelect(i, i+1, hte);
  478.             return(-1);
  479.         }
  480.         l = l * 16 + c;
  481.         i++;
  482.     }
  483.     return(l);
  484. }
  485.  
  486.